﻿using System;
using Csla;
using BizControl.Library.BusinessObjects.Activities;
using BizControl.Library.Criterias;
using System.Collections.Generic;

namespace BizControl.Library.BusinessObjects.Participants
{
  [Serializable()]
  public class ParticipantList : ReadOnlyListBase<ParticipantList, ParticipantInfo>
  {
    public ParticipantInfo this[Guid participantIdSearchFor]
    {
      get
      {
        foreach (ParticipantInfo participant in this)
        {
          if (participant.Id == participantIdSearchFor)
            return participant;
        }
        return null;
      }
    }

    public ParticipantInfo this[ParticipantInfo participantSearchFor]
    {
      get
      {
        foreach (ParticipantInfo participant in this)
        {
          if (participant.Id == participantSearchFor.Id)
            return participant;
        }
        return null;
      }
    }

    #region  Factory Methods

    public static ParticipantList GetParticipantList(ActivityBO activity, bool includeEmptyParticipant = false)
    {
      return DataPortal.Fetch<ParticipantList>(new ActivityAndBoolCriteria(activity, includeEmptyParticipant));
    }

    #endregion

    #region  Data Access

    private void DataPortal_Fetch(ActivityAndBoolCriteria criteria)
    {
      RaiseListChangedEvents = false;
      IsReadOnly = false;
      if (criteria.Activity != null)
      {
        foreach (var participant in criteria.Activity.Participants)
          this.Add(new ParticipantInfo(participant));
      }
      if (criteria.Bool)
        this.Add(new ParticipantInfo(ParticipantBO.GetEmptyParticipant()));
      IsReadOnly = true;
      RaiseListChangedEvents = true;
    }

    #endregion

  }
}
